#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/wait.h>

const char *FILENAME = "result.txt";

void culculation(char& option,float a,float b,FILE* file)
{
    int result = 0;
    switch (option)
    {
    case  '+':
        result = a+b;
        fprintf(file,"%d,%d,%d\n",getpid(),getppid(),result);
        break;
     case  '-':
        result = a-b;
        fprintf(file,"%d,%d,%d\n",getpid(),getppid(),result);
        break;
     case  '*':
        result = a*b;
        fprintf(file,"%d,%d,%d\n",getpid(),getppid(),result);
        break;
     case  '/':
        result = a/b;
        fprintf(file,"%d,%d,%d\n",getpid(),getppid(),result);
        break;
    default:
        break;
    }
}

//计算器程序
int main()
{
    pid_t pid[4];
    char oper[4] = {'+','-','*','/'};
    for(int i = 0;i<4;i++)
    {
        pid[i] = fork();
        if(pid[i] == 0)
        {
            FILE* file=fopen(FILENAME,"a");
            culculation(oper[i],4,4,file);
            fclose(file);
            exit(1);
        }
    }

    for(int i = 0;i<4;i++)
    {
        wait(NULL);
    }

    return 0;
}